/*****************************************************************************************
This file is a part of Arise library.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

Arise Engine		PortalClipper.h

					E-mail:		arise_engine@mail.ru

					Origin by:	DK Engine 0.019,
								dev (avon.dn.ua@gmail.com), Agressor (agress0r@mail.ru)

created:			20.11.2004
last changes:		30.04.2007
*****************************************************************************************/
#pragma once
#include "ClipBuffer.h"
#include "Sector.h" 
#include "Portal.h" 
#include "OctreeNode.h" 
#include "GraphicObject.h" 
#include "..\renderer\Camera.h" 
#include "IScene.h" 


class PortalClipper
{
private:

	IScene*		m_parent_scene;
	ClipManager	m_clip_manager;
	Portal*		m_collided_portal;
	Sector*		m_current_sector;
	Camera*		m_current_camera;

	bool m_is_collided_last_frame;
	bool m_is_outdoor_visible;
	UINT m_num_visible_sectors;
	UINT m_num_visible_objects;

	void AddSectorToRender(Sector* sector);
	void AddGraphicObjectToRender(GraphicObject* object);
	void AddOctreeNodeToRender(OctreeNode* node);
	void CheckPortal(Frustum* frustum, Portal* portal, bool is_skip_outdoor, const D3DXVECTOR3 &position);
	void CheckSector(Sector* sector, Frustum* frustum, bool is_skip_outdoor, const D3DXVECTOR3 &position, Portal* colided_portal = 0);
	void AddPortalToVisible(Portal* portal, bool is_skip_outdoor);
	void FindCollidedPortal(const D3DXVECTOR3 &position, float radius);
	Portal* FindCollidePortalInSecor(Sector* sector, const D3DXVECTOR3 &position, float radius);
	void FindCollidePortalsInNode(Sector* sector, OctreeNode* node, const D3DXVECTOR3 &position, float radius);
	void FindVisibleNodes(Sector* sector, OctreeNode* node, Frustum* frustum, bool is_skip_outdoor, const D3DXVECTOR3 &position, Portal* colided_portal = 0);
	void ClearBeforeNewFrame();
	
	void SetCurrentSector(Sector* sector)	{if(sector) m_current_sector = sector; m_collided_portal = 0;}

public:
	PortalClipper();
	~PortalClipper();

	std::vector<Sector*>			m_sector_list;
	std::vector<Sector*>			m_visible_sectors;	
	std::vector<GraphicObject*>		m_visible_objects;	
	
	void Init(IScene* scene, const std::vector<Sector*> &sector_list);
	void Update(Camera* current_camera);
	void ApplyCurrentSector(UINT sector_id);
	void Clear();

	bool		IsOutdoorVisible()		{return m_is_outdoor_visible;}
	Sector*		GetCurrentSector()		{return m_current_sector;}
	UINT		GetNumVisibleSectors()	{return m_num_visible_sectors;}
	UINT		GetNumVisibleObjects()	{return m_num_visible_objects;}
	Portal*		GetCollidedPortal()		{return m_collided_portal;}
};


